﻿bob.ns.setNs('bob.obj.tests', {

    TestObj: function () {

        this.test_canExtractInterface = function () {
            bob.assert.throwsError(function () { bob.obj.canExtractInterface(); }, '1');
            bob.assert.throwsError(function () { bob.obj.canExtractInterface(null, {}); }, '2');
            bob.assert.throwsError(function () { bob.obj.canExtractInterface({}, null); }, '2');

            var obj = {};
            var interface = { prop: 0 };
            bob.assert.throwsError(function () { bob.obj.canExtractInterface(obj, interface); }, '4');

            var obj = {};
            var interface = { method1: function () { } };
            bob.assert.isFalse(bob.obj.canExtractInterface(obj, interface), '5');


            var obj = { method1: function () { } };
            var interface = {};
            var objInterface = bob.obj.extractInterface(obj, interface);
            bob.assert.isTrue(bob.obj.canExtractInterface(obj, interface), '5');


            var obj = { method1: function () { }, method2: function () { } };
            var interface = { method2: function () { } };
            var objInterface = bob.obj.extractInterface(obj, interface);
            bob.assert.isTrue(bob.obj.canExtractInterface(obj, interface), '5');


            var obj = {
                field1: 10,
                method1: function (plus) {
                    return this.field1 + plus;
                },
                method2: function () {
                }
            };
            var interface = { method1: function () { } };
            bob.assert.isTrue(bob.obj.canExtractInterface(obj, interface), '5');

        };

        this.test_extractInterface = function () {

            bob.assert.throwsError(function () { bob.obj.extractInterface(); }, '1');
            bob.assert.throwsError(function () { bob.obj.extractInterface(null, {}); }, '2');

            var obj = {};
            var interface = { prop: 0 };
            bob.assert.throwsError(function () { bob.obj.extractInterface(obj, interface); }, '4');

            var obj = {};
            var interface = { method1: function () { } };
            bob.assert.throwsError(function () { bob.obj.extractInterface(obj, interface); }, '5');


            var obj = { method1: function () { } };
            var interface = {};
            var objInterface = bob.obj.extractInterface(obj, interface);
            bob.assert.isTrue(bob.utils.isObject(objInterface), '6');
            bob.assert.isTrue(bob.collections.extensions.equals.call(interface, objInterface,
            function (item1, item2, index1, index2) {
                return index1 == index2;
            }), '7');


            var obj = { method1: function () { }, method2: function () { } };
            var interface = { method2: function () { } };
            var objInterface = bob.obj.extractInterface(obj, interface);
            bob.assert.isTrue(bob.utils.isObject(objInterface), '8');
            bob.assert.isTrue(bob.collections.extensions.equals.call(interface, objInterface,
            function (item1, item2, index1, index2) {
                return index1 == index2;
            }), '9');


            var obj = {
                field1: 10,
                method1: function (plus) {
                    return this.field1 + plus;
                },
                method2: function () {
                }
            };
            var interface = { method1: function () { } };
            var objInterface = bob.obj.extractInterface(obj, interface);
            bob.assert.isTrue(bob.utils.isObject(objInterface), '10');
            bob.assert.isTrue(bob.collections.extensions.equals.call(interface, objInterface,
            function (item1, item2, index1, index2) {
                return index1 == index2;
            }), '11');
            bob.assert.equals(11, objInterface.method1(1));

            //null interface (PASSING null).
            var obj = {
                field1: 10,
                method1: function (plus) {
                    return this.field1 + plus;
                },
                method2: function () {
                }
            };
            //this is the expected interface.
            var interface = { method1: function () { }, method2: function () { } };
            var objInterface = bob.obj.extractInterface(obj, null);
            bob.assert.isTrue(bob.utils.isObject(objInterface), '12');
            //compare actual to expected interface.
            bob.assert.isTrue(bob.collections.extensions.equals.call(interface, objInterface,
            function (item1, item2, index1, index2) {
                return index1 == index2;
            }), '13');
            bob.assert.equals(11, objInterface.method1(1));

        };

    }

});